const Model = require('../models/hanziCharacterModel')

const getAllHanziCharacters = async (ctx) => {
  ctx.body = { success: true, data: await Model.getAllHanziCharacters() }
}

const getHanziCharacterById = async (ctx) => {
  const { id } = ctx.params
  const hanziCharacter = await Model.getHanziCharacterById(id)
  if (!hanziCharacter) ctx.throw(404, 'Hanzi Character not found')
  ctx.body = hanziCharacter
}

const createHanziCharacter = async (ctx) => {
  const hanziData = ctx.request.body
  const hanziId = await Model.createHanziCharacter(hanziData)
  ctx.status = 201
  ctx.body = { id: hanziId, ...hanziData }
}

const updateHanziCharacter = async (ctx) => {
  const { id } = ctx.params
  const hanziData = ctx.request.body
  await Model.updateHanziCharacter(id, hanziData)
  ctx.body = { success: true }
}

const deleteHanziCharacter = async (ctx) => {
  const { id } = ctx.params
  await Model.deleteHanziCharacter(id)
  ctx.status = 204
}

module.exports = {
  getAllHanziCharacters,
  getHanziCharacterById,
  createHanziCharacter,
  updateHanziCharacter,
  deleteHanziCharacter,
}